package com.ja.openchat.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.google.gson.Gson;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentNameAuthenBinding;
import com.ja.openchat.databinding.FragmentNameAuthenNumberBinding;
import com.ja.openchat.viewmodels.NameAuthenNumberViewModel;
import com.ja.openchat.viewmodels.NameAuthenViewModel;
import com.liaobeicms.http.bean.NameAuthenBean;
import com.liaobeicms.http.bean.NameAuthenInfoBean;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.ProvinceBean;
import com.sahooz.library.Country;
import com.zy.openchat.util.MediaUtils;
import com.zy.openchat.util.glide.GlideApp;

import java.io.File;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

//实名认证界面
public class NameAuthenNumberFragment extends BaseFragment<FragmentNameAuthenNumberBinding, NameAuthenNumberViewModel> {

    private String name;
    private String number;
    private int index = 0;
    private String fontPhoto;
    private String backPhoto;

    private NameAuthenInfoBean mNameAuthenBean;

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null) {
            String nameAuthenBean = bundle.getString("nameAuthenBean");
            if (!TextUtils.isEmpty(nameAuthenBean)) {
                mNameAuthenBean = new Gson().fromJson(nameAuthenBean, NameAuthenInfoBean.class);
            } else {
                name = bundle.getString("name");
                number = bundle.getString("number");
            }
        }
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_name_authen_number;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        super.initView();
        BarUtils.setStatusBarLightMode(getActivity(), false);
        BarUtils.setStatusBarColor(getActivity(), getResources().getColor(R.color.color_f2f9f9));
    }

    @Override
    public void initData() {
        super.initData();
        if (mNameAuthenBean != null) {
            binding.tvName.setText(mNameAuthenBean.username);
            binding.tvNumber.setText(mNameAuthenBean.idcardno);
            fontPhoto = mNameAuthenBean.idcardimg_p;
            backPhoto = mNameAuthenBean.idcardimg_n;
            GlideApp.with(getActivity()).load(mNameAuthenBean.idcardimg_p).into(binding.ivFront);
            GlideApp.with(getActivity()).load(mNameAuthenBean.idcardimg_n).into(binding.ivBack);
            if ("0".equals(mNameAuthenBean.status)) {
                binding.tvConfirm.setText("信息审核中");
                binding.tvConfirm.setBackgroundResource(R.drawable.login_btn_bg_green_90_unselect);
            } else if ("1".equals(mNameAuthenBean.status)) {
                binding.tvConfirm.setText("认证通过");
                binding.tvConfirm.setBackgroundResource(R.drawable.login_btn_bg_green_90_unselect);
            } else if ("2".equals(mNameAuthenBean.status)) {
                binding.tvName.setEnabled(true);
                binding.tvNumber.setEnabled(true);
                binding.tvName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6), cheseInputFilter});
                binding.tvConfirm.setText("重新提交");
                binding.tvConfirm.setBackgroundResource(R.drawable.login_btn_bg_green_90);
            }
        } else {
            if (!TextUtils.isEmpty(name)) {
                binding.tvName.setText(name);
            }
            if (!TextUtils.isEmpty(number)) {
                binding.tvNumber.setText(number);
            }
        }
        if (mNameAuthenBean == null || "2".equals(mNameAuthenBean.status)) {
            binding.ivFront.setOnClickListener(view -> {
                index = 1;
                MediaUtils.choosePhotoFg(NameAuthenNumberFragment.this, true);
            });
            binding.ivBack.setOnClickListener(view -> {
                index = 2;
                MediaUtils.choosePhotoFg(NameAuthenNumberFragment.this, true);
            });
            binding.tvConfirm.setOnClickListener(view -> {
                name = binding.tvName.getText().toString();
                number = binding.tvNumber.getText().toString();
                viewModel.submitData(fontPhoto, backPhoto, name, number);
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {

        } else {
            List<String> photos = MediaUtils.getResultAlbumData(getActivity(), requestCode, resultCode, data);
            if (photos != null && photos.size() > 0) {
                if (index == 1) {
                    fontPhoto = photos.get(0);
                    GlideApp.with(getActivity()).load(new File(fontPhoto)).into(binding.ivFront);
                } else {
                    backPhoto = photos.get(0);
                    GlideApp.with(getActivity()).load(new File(backPhoto)).into(binding.ivBack);
                }
            }
        }
    }

    InputFilter cheseInputFilter = (source, start, end, dest, dstart, dend) -> {
        for (int i = start; i < end; i++) {
            if (!isChinese(source.charAt(i))) {
                return "";
            }
        }
        return null;
    };

    private boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT;
    }
}